package com.songcha.library_business.mvvm

import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.library_business.api.ApiManager
import com.songcha.library_business.bean.banner.BannerBean
import com.songcha.library_business.bean.book.BookChapterCatalogBean
import com.songcha.library_business.bean.configuration.SystemConfigurationBean
import com.songcha.library_business.bean.user.UserInfoBean
import com.songcha.library_network.bean.BaseBean
import io.reactivex.Observable
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONArray
import org.json.JSONObject

/***
 * common repository
 */
open class BusinessRepository:BaseRepository() {
    fun getUserInfo(): Observable<UserInfoBean> {
        return ApiManager.api.getUserInfo()
    }

    fun getBanner(type:Int):Observable<BannerBean>{
        return ApiManager.api.getBanner(type)
    }

    fun getBookChapterCatalog(bookId:Int): Observable<BookChapterCatalogBean> {
        return ApiManager.Api_timeout60.getBookChapterCatalog(bookId)
    }

    fun addToBookShelf(bookId: Int): Observable<BaseBean> {
        val json= JSONObject().apply {
            put("bookId",bookId)
        }
        return ApiManager.api.addToBookShelf(json.toString().toRequestBody())
    }


    fun delBookShelf(ids:MutableList<String>): Observable<BaseBean> {
        val json= JSONObject().apply {
            val idArr=JSONArray()
            for(i in ids.indices){
                val id=ids[i]
                idArr.put(id)
            }
            put("ids",idArr)
        }
        return ApiManager.api.delBookShelf(json.toString().toRequestBody())
    }

    fun updateClientIp():Observable<BaseBean>{
        return ApiManager.api.updateClientIp()
    }

    fun getSystemConfiguration():Observable<SystemConfigurationBean>{
        return ApiManager.api_timeout15.getSystemConfiguration()
    }




}